package project.teg.games.game.gestures;

import java.util.Date;
import java.util.Random;

import project.teg.common.Border;
import project.teg.common.types.BorderType;
import project.teg.common.types.ScreenFieldType;
import project.teg.configuration.Gestures;
import project.teg.configuration.Global;

import android.graphics.Point;
import android.graphics.PointF;
import android.util.Log;

public class Methods {

	Random random = new Random(new Date().getTime());

	public Point returnBeginigOfGesture() {
		Point p = new Point();
		p.x = random.nextInt(Gestures.gridColumns);
		p.y = random.nextInt(Gestures.gridRows);
		return p;
	}

	public static Border findClosestBorder(PointF point) {

		Border border = new Border();
		border.distanceFromBorder = Global.screenSizePx.x;
		float gore = point.y;
		float dolje = Global.screenSizePx.y - point.y;
		float lijevo = point.x;
		float desno = Global.screenSizePx.x - point.x;

		if (border.distanceFromBorder > gore) {
			border.distanceFromBorder = gore;
			border.borderType = BorderType.Up;
		}
		if (border.distanceFromBorder > dolje) {
			border.distanceFromBorder = dolje;
			border.borderType = BorderType.Down;
		}
		if (border.distanceFromBorder > lijevo) {
			border.distanceFromBorder = lijevo;
			border.borderType = BorderType.Left;
		}
		if (border.distanceFromBorder > desno) {
			border.distanceFromBorder = desno;
			border.borderType = BorderType.Right;
		}
		return border;
	}

	public static ScreenFieldType determinePointScreenFiled(PointF l) {
		if (l.x < Global.screenSizePx.x / 3)
			// left
			if (l.y < Global.screenSizePx.y / 3)
				// up
				return ScreenFieldType.UpLeft;
			else if (l.y < 2 * Global.screenSizePx.y / 3)
				// center
				return ScreenFieldType.Left;
			else
				// down
				return ScreenFieldType.DownLeft;

		else if (l.x < 2 * Global.screenSizePx.x / 3)
			// center
			if (l.y < Global.screenSizePx.y / 3)
				// up
				return ScreenFieldType.Up;
			else if (l.y < 2 * Global.screenSizePx.y / 3)
				// center
				return ScreenFieldType.Center;
			else
				// down
				return ScreenFieldType.Down;
		else
		// right
		if (l.y < Global.screenSizePx.y / 3)
			// up
			return ScreenFieldType.UpRight;
		else if (l.y < 2 * Global.screenSizePx.y / 3)
			// center
			return ScreenFieldType.Right;
		else
			// down
			return ScreenFieldType.DownRight;
	}

	public static float calculateDistance(PointF a, PointF b) {
		return (float) Math.sqrt(Math.pow(a.x - b.x, 2)
				+ Math.pow(a.y - b.y, 2));
	}

	public static boolean checkIfCheckPointIsNear(CheckPoint c, PointF p) {
		float distance = calculateDistance(c.location, p);
		if (distance < Gestures.minimalDistanceToCheckPoint) {

			c.isHit = true;
			return true;
		} else
			return false;
	}
}
